import { Component, OnInit } from '@angular/core';
import {HttpClient} from "@angular/common/http"
import {Router,ActivatedRoute} from "@angular/router"
@Component({
  selector: 'app-contact-edit',
  templateUrl: './contact-edit.component.html',
  styleUrls: ['./contact-edit.component.css']
})
export class ContactEditComponent implements OnInit {

  constructor(
    private http:HttpClient,
    private router:Router,
    private route:ActivatedRoute
  ) { }
public contact={
  name: '',
    email: '',
    phone: '',
    id: 0
}
  ngOnInit(): void {
    const id = this.route.snapshot.params.id
    this.getContactById(id)
    
  }
  getContactById(id){
    this.http.get(`http://localhost:3000/contacts/${id}`//,{headers: new HttpHeaders({
    //   "X-Access-Token":token})
    
    ).toPromise().then((data:any)=>{
console.log(data)
this.contact = data

    })
  }
  editContact(){
    this.http.patch(`http://localhost:3000/contacts/${this.contact.id}`,this.contact).toPromise().then((data)=>{
      console.log(data)
    }).catch((e)=>{
      console.log(e)
    })
    this.router.navigate(['/contact'])
  }

}
